package service

import (
	"context"
	"time"

	"gitee.com/jiebaiyou/formflow/model"
)

// taskService 实现了model.TaskService接口, 提供任务相关的业务逻辑
type taskService struct {
	taskRepository model.TaskRepository
	contextTimeout time.Duration
}

// NewTaskService 创建并返回一个实现了model.TaskService接口的实例
func NewTaskService(taskRepository model.TaskRepository, timeout time.Duration) model.TaskService {
	return &taskService{
		taskRepository: taskRepository,
		contextTimeout: timeout,
	}
}

// Create 创建新任务
func (tu *taskService) Create(c context.Context, task *model.Task) error {
	ctx, cancel := context.WithTimeout(c, tu.contextTimeout)
	defer cancel()
	return tu.taskRepository.Create(ctx, task)
}

// FetchByUserID 获取指定用户的所有任务
func (tu *taskService) FetchByUserID(c context.Context, userID uint) ([]model.Task, error) {
	ctx, cancel := context.WithTimeout(c, tu.contextTimeout)
	defer cancel()
	return tu.taskRepository.FetchByUserID(ctx, userID)
}
